package idiet.dialog;

import idiet.Activator;
import idiet.bean.Unit;
import idiet.service.FoodIngredientElementUnitService;

import org.apache.log4j.Logger;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;
import java.util.ResourceBundle;

public class DialogAddUnit extends TitleAreaDialog {
	private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("idiet.dialog.messages"); //$NON-NLS-1$
	private Logger logger =Logger.getLogger(DialogAddUnit.class);
	private FoodIngredientElementUnitService foodIngredientElementUnitService=(FoodIngredientElementUnitService)Activator.getApplicationContext().getBean("foodIngredientElementUnitService");
	private Text textName;
	private Text textEnName;
	private Text textComment;
	//private final String nameError="name is not empty ! ";
	//private final String nameError="单位名不能为空！";
	//private final String ennameError="en name is not empty ! ";
	//private final String tips="Please enter new unit information ";
	//private final String tips="请输入单位信息";
	public DialogAddUnit(Shell parentShell) {
		super(parentShell);
		setHelpAvailable(false);
		setShellStyle(SWT.CLOSE | SWT.MIN | SWT.TITLE);
		//this.setErrorMessage(newErrorMessage)
		// TODO Auto-generated constructor stub
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		setMessage(BUNDLE.getString("DialogAddUnit.this.message"));
		setTitle(BUNDLE.getString("DialogAddUnit.this.title")); //$NON-NLS-1$
		Composite composite =(Composite) super.createDialogArea(parent);
		
		Composite composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setLayout(new GridLayout(2, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Label lblNewLabel = new Label(composite_1, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText(BUNDLE.getString("DialogAddUnit.lblNewLabel.text")); //$NON-NLS-1$
		
		textName = new Text(composite_1, SWT.BORDER);
		textName.setEnabled(true);
		textName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if(textName.getText().length()==0){
					setErrorMessage(BUNDLE.getString("DialogAddUnit.no.name.error"));
				}else{
					setErrorMessage(null);
					//setMessage(BUNDLE.getString("DialogAddUnit.this.message"));
					getButton(OK).setEnabled(true);
				}
			}
		});
		textName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		//textName.addModifyListener(listener)
		Label lblNewLabel_1 = new Label(composite_1, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_1.setText(BUNDLE.getString("DialogAddUnit.lblNewLabel_1.text")); //$NON-NLS-1$
		
		textEnName = new Text(composite_1, SWT.BORDER);
		textEnName.setEnabled(true);
		textEnName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		/*textEnName.addModifyListener(new ModifyListener(){

			@Override
			public void modifyText(ModifyEvent e) {
				// TODO Auto-generated method stub
				if()
			}
			
		});*/
		Label lblNewLabel_2 = new Label(composite_1, SWT.WRAP);
		lblNewLabel_2.setAlignment(SWT.RIGHT);
		GridData gd_lblNewLabel_2 = new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1);
		gd_lblNewLabel_2.widthHint = 59;
		lblNewLabel_2.setLayoutData(gd_lblNewLabel_2);
		lblNewLabel_2.setText(BUNDLE.getString("DialogAddUnit.lblNewLabel_2.text")); //$NON-NLS-1$
		
		textComment = new Text(composite_1, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		textComment.setEnabled(true);
		textComment.setTextLimit(1024);
		GridData gd_textComment = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_textComment.heightHint = 117;
		textComment.setLayoutData(gd_textComment);
		new Label(composite_1, SWT.NONE);
		
		Label lblsizeIsLimited = new Label(composite_1, SWT.NONE);
		lblsizeIsLimited.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblsizeIsLimited.setAlignment(SWT.RIGHT);
		//lblsizeIsLimited.setText("(Size is limited to 1024)");
		lblsizeIsLimited.setText(BUNDLE.getString("DialogAddUnit.lblsizeIsLimited.text")); //$NON-NLS-1$
		//this.getButton(OK).setEnabled(false);
		return composite;
	}

	@Override
	protected void cancelPressed() {
		// TODO Auto-generated method stub
		super.cancelPressed();
	}

	@Override
	protected void okPressed() {
		// TODO Auto-generated method stub
		try {
			if(this.textName.getText().length()==0){
				Unit unit=new Unit();
				unit.setName(this.textName.getText());
				unit.setEnName(this.textEnName.getText());
				unit.setComment(this.textComment.getText());
				this.foodIngredientElementUnitService.createFoodIngredientElementUnit(unit);
				if(logger.isDebugEnabled()){
					logger.debug("create unit : "+unit.toString());
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e);
			e.printStackTrace();
		}
		super.okPressed();
	}
	
}
